/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package com.icerhe.trigger;

import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.preference.RingtonePreference;
import android.util.AttributeSet;

/**
 * @author icer
 * 系统的RingtonePreference是没有铃声的get/set方法的,所以扩展系统类,以设置和取得所选的铃声
 */
public class TaskRingtonePreference extends RingtonePreference {
	
	private Uri ringtone;
	
	public TaskRingtonePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
	
    @Override
    protected void onSaveRingtone(Uri ringtoneUri) {
    	setRingtone(ringtoneUri);
    }
	
	@Override
    protected Uri onRestoreRingtone() {
        if (RingtoneManager.isDefault(ringtone)) {
            return RingtoneManager.getActualDefaultRingtoneUri(getContext(),
                    RingtoneManager.TYPE_ALARM);
        }
        return ringtone;
    }
	
    public Uri getRingtone() {
        return ringtone;
    }
    
    public void setRingtone(Uri ringtoneUri) {
    	ringtone = ringtoneUri;
        if (ringtoneUri != null) {
            final Ringtone r = RingtoneManager.getRingtone(getContext(), ringtoneUri);
            if (r != null) {
                setSummary(r.getTitle(getContext()));
            }
        } else {
            setSummary(R.string.ringtone_silent);
        }
    }
}
